package com.epsoft.odin7cj.flow.api.formtmpl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.epsoft.odin7cj.core.dto.RetData;
import com.epsoft.odin7cj.core.exception.BaseException;
import com.epsoft.odin7cj.core.model.PageBean;
import com.epsoft.odin7cj.core.util.JhkjUUIDUtil;
import com.epsoft.odin7cj.flow.dao.formtmpl.FormTmplDao;
import com.epsoft.odin7cj.flow.enums.FlowRetCodeEnum;
import com.epsoft.odin7cj.flow.model.deploy.FlowDeployment;
import com.epsoft.odin7cj.flow.model.formtmpl.FormTmpl;
import com.epsoft.odin7cj.flow.util.BpmnBeanUtils;
import com.epsoft.odin7cj.flow.util.BpmnPicUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * DESC: 表单模板 <br>
 * @author 胡义振
 * @date 2020年5月20日
 */
@RestController
@RequestMapping("/api/flow/form/tmpl")
public class FormTmplApi {

	@Autowired
	private FormTmplDao formTmplDao;
	@Autowired
	private RepositoryService repositoryService;
	
    /**
     * Desc: 查询所有流程发布配置表
     * @param auditDeployment  
     * @return
     * @throws Exception
     */
	@RequestMapping("/getFormTmplList")
    public RetData<PageInfo<FormTmpl>> doGetFormTmplList(FormTmpl formTmpl,PageBean pageBean) throws Exception {
		RetData<PageInfo<FormTmpl>> retData = new RetData<PageInfo<FormTmpl>>();
		PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		List<FormTmpl> listDeployLib = formTmplDao.selectList(formTmpl);
		PageInfo<FormTmpl> pageInfo = new PageInfo<FormTmpl>(listDeployLib);
		retData.setRetBody(pageInfo);
    	return retData;
    }
	
	@RequestMapping("/getFormTmpl")
    public RetData<FormTmpl> doGetFormTmpl(String id) throws Exception {
		RetData<FormTmpl> retData = new RetData<FormTmpl>();
		FormTmpl formTmpl = formTmplDao.selectById(id);
    	retData.setRetBody(formTmpl);
    	return retData;
    }
	
	@RequestMapping("/addFormTmpl")
    public RetData<String> doSaveFormTmpl(FormTmpl formTmpl) throws Exception {
		RetData<String> retData = new RetData<String>();
		formTmpl.setId(JhkjUUIDUtil.getUUID());
		formTmplDao.insertOne(formTmpl);
    	retData.setRetBody("");
    	return retData;
    }
	
	@RequestMapping("/editFormTmpl")
    public RetData<String> doEditFormTmpl(FormTmpl formTmpl) throws Exception {
		RetData<String> retData = new RetData<String>();
		formTmplDao.updateById(formTmpl);
    	retData.setRetBody("");
    	return retData;
    }
	
	@RequestMapping("/delFormTmpl")
    public RetData<String> doDelFormTmpl(String tmplId) throws Exception {
		RetData<String> retData = new RetData<String>();
		formTmplDao.deleteById(tmplId);
    	retData.setRetBody("");
    	return retData;
    }
	
	/**
	 * DESC: 流程发布
	 * @param deployName 流程的名称 
	 * @param bpmnPath 资源文件的 bpmn 目录中的资源（不含后缀）
	 * @return
	 */
	@RequestMapping("/deploy")
	public RetData<FlowDeployment> deployBpmn(String tmplId)  throws Exception {
		RetData<FlowDeployment> retData = new RetData<FlowDeployment>();
		
		FormTmpl formTmpl = formTmplDao.selectById(tmplId);
		if(formTmpl==null) {
			throw new BaseException(FlowRetCodeEnum.RET_CODE_0501001.getCode(),FlowRetCodeEnum.RET_CODE_0501001.getMessage());
		}
		if(formTmpl.getBpmnXml()==null || formTmpl.getBpmnXml().equals("")) {
			throw new BaseException(FlowRetCodeEnum.RET_CODE_0501002.getCode(),FlowRetCodeEnum.RET_CODE_0501002.getMessage());
		}
		Deployment deployment = null;
		try
		{
			InputStream bpmnInputStream = new ByteArrayInputStream(formTmpl.getBpmnXml().getBytes("utf-8"));
			ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
			BpmnPicUtils.svgConvertToPng(formTmpl.getBpmnSvg(), outPutStream);
			InputStream pngInputStream = new ByteArrayInputStream(outPutStream.toByteArray());
			deployment = repositoryService.createDeployment()
					.name(formTmpl.getTmplName())
					.addInputStream(formTmpl.getTmplKey()+".bpmn", bpmnInputStream)
					.addInputStream(formTmpl.getTmplKey()+".png", pngInputStream)
					
					.addClasspathResource("form/start.html")
					.addClasspathResource("form/fill.html")
					.addClasspathResource("form/audit_dept.html")
					.addClasspathResource("form/audit_fina.html")
					
					.deploy();
		}
		catch(Exception er) {
			System.out.println("\n 流程发布出现异常：");
			er.printStackTrace();
		}
		retData.setRetBody(BpmnBeanUtils.convertToFlowDeployment(deployment));
		return retData;
	}
	
}
